/**
 * Green.Toolbar
 */

Green.Toolbar = Green.Toolbar || {};
Green.Paginate = Green.Paginate || {};
(function ($) {
    Green.attach("Toolbar",{
        setup:function(){
            $("#toolbar-control button").each(function(){
                var $element = $(this);
                var action = $element.attr("rel");
                var task =  $element.attr("title");
                $element.button({
                    icons:{
                        primary:"icon-"+task
                    }
                }).removeAttr("title").click(function(){
                    if($.isFunction(Green.Toolbar[action]) && Green.Toolbar[action]($element)){
                        $("#task").val(task);
                        $("#paginate").submit();
                    }
                });
            });
        }
    });
    var checkPaginate = function(){
        return $('.row-id:checked').length;
    };
    $.extend(Green.Toolbar, {
        /**
         * Add
         */
        add:function($element){
            return true;
        },
        /**
         * Copy
         */
        copy:function($element){
            return true;
        },
        /**
         * Edit
         */
        edit:function($element){
            return true;
        },
        /**
         * multiple remove
         */
        removes:function($element){
            return confirm(Green.t("Are you sure to delete all %s items selected?",length));
        },
        /**
         * remove
         */
        remove:function($element){
            return confirm(Green.t("Are you sure to delete?"));
        },
        /**
         * save
         */
        save:function($element){
            if(Green.trigger("validate"),[ $("#paginate")]){
                return true;
            }
            return false;
        },
        cancel:function($element){
            return true;
        }
    });
    
    Green.attach("Paginate",{
        setup:function(){
            /* Sidebar */
            $( "#topbar .toggle" ).click(function(){
                var $element = $(this).toggleClass('toggle-active');
                if($element.hasClass('toggle-active')){
                    $('#content').addClass('topbar-drawer');
                    $('#topbar-drawer').show();
                }else{
                    $('#content').removeClass('topbar-drawer');
                    $('#topbar-drawer').hide();
                }
            });
            $(".table-head a").each(function(i,element){
                i = "carat-2-n-s";
                if(element.className){
                    i = element.className=="asc" ?  "triangle-1-s" :"triangle-1-n";
                }
                $(element).prepend($("<span/>").addClass("ui-icon ui-icon-"+i));
            });
            
            
            // Table select
            var $el = $('.table-select input'),$val = $('.table-content .'+ $el.val());
            $el.click(function(){
                $val.prop('checked',$el.prop('checked'));
                check();
            });
            var check = function(){
                var is = false;
                $val.each(function(){
                    var $el = $(this);
                    if($el.prop('checked')){
                        is  = true;
                        $el.closest('tr').addClass('table-hover');
                    }else{
                        $el.closest('tr').removeClass('table-hover');
                    }
                });
                $('#toolbar button[rel="removes"],#toolbar button[rel="copy"],#toolbar button[rel="edit"]').each(function(){
                    if(is){
                        $(this).show();
                    }else{
                        $(this).hide();
                    }
                });
            }
            $val.change(check);
            check();
            
            // Filter
            $('.filter-keyword .toggle').click(function(){
                if($('#filter-options').is(':hidden')){
                    $('#filter-options').slideDown('slow');
                }
                else{
                    $('#filter-options').slideUp('slow');
                }
                return false;
            });
            
            $(document).bind('click',function(){
                $('#filter-options').slideUp('slow');
            });
            $('#filter-options').click(function(e) {
                e.stopPropagation();
            });
            
            $('#filter-close').click(function(){
                $('#filter-options').slideUp('slow');
                return false;
            }); 
            $('.panel .legend').append('<a href="#" class="wd-arrow"></a>');
            $('.translate-title').prepend('<span class="ui-icon ui-icon-circle-minus"></span>');
            $('.wd-arrow').click(function(){
                $(this).parent().siblings('.rowset').slideToggle('slows');
                $(this).toggleClass('wd-arrow-active');
                $(this).parent().toggleClass('wd-none-margin');
                return false;
            }); 
            $('.translate-title').click(function(){
                $(this).children('.ui-icon').toggleClass('ui-icon-circle-minus').toggleClass('ui-icon-circle-plus');
                $(this).siblings('.translate-content').slideToggle();
                return false;
            });
   
        }
    });
    $.extend(Green.Paginate, {
        /**
         * To top
         */
        toTop:function(options){
            if(!$.support.positionFixed){
                return;
            }
            var defaults = {
                text: "To Top",
                min: 200,
                inDelay:600,
                outDelay:400,
                speed: 700,
                easing: "linear"
            };

            var settings = $.extend(defaults, options);
		
            $("body").append("<a href=\"#\" id=\"to-top\">"+settings.text+"</a>");
            $("#to-top").hide().click(function(){
                $("html, body").stop().animate({
                    scrollTop:0
                }, settings.speed, settings.easing);
                $("#to-top-hover", this).stop().animate({
                    "opacity": 0
                }, settings.inDelay, settings.easing);
                return false;
            })
            .prepend("<span id=\"to-top-hover\"></span>")
            .hover(function() {
                $("#to-top-hover", this).stop().animate({
                    "opacity": 1
                }, 600, "linear");
            }, function() { 
                $("#to-top-hover", this).stop().animate({
                    "opacity": 0
                }, 700, "linear");
            });
					
            $(window).scroll(function() {
                var sd = $(window).scrollTop();
                if(typeof document.body.style.maxHeight === "undefined") {
                    $("#to-top").css({
                        "position": "absolute",
                        "top": $(window).scrollTop() + $(window).height() - 50
                    });
                }
                if ( sd > settings.min ) {
                    $("#to-top").fadeIn(settings.inDelay);
                }
                else {
                    $("#to-top").fadeOut(settings.Outdelay);
                }
				
            });
        }
    });

})(jQuery);